// import 'package:enjoy_plus/pages/home/index.dart';

import 'package:enjoy_plus/pages/404.dart';
import 'package:enjoy_plus/pages/home/detail.dart';
import 'package:enjoy_plus/pages/house/building_list.dart';
import 'package:enjoy_plus/pages/house/house_detail.dart';
import 'package:enjoy_plus/pages/house/house_form.dart';
import 'package:enjoy_plus/pages/house/house_list.dart';
import 'package:enjoy_plus/pages/house/location_list.dart';
import 'package:enjoy_plus/pages/house/room_list.dart';
import 'package:enjoy_plus/pages/login/index.dart';
import 'package:enjoy_plus/pages/profile/index.dart';
import 'package:enjoy_plus/pages/tab_bar_page.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const TabBarPage(),
      '/detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    // 根据条件配置动态路由
    onGenerateRoute: (settings) {
      // 1. 获取token
      final tokenManager = TokenManager();
      final token = tokenManager.getToken() ?? '';

      // 2. token没值且去的不是登录页
      if (token.isEmpty && settings.name != '/login') {
        return MaterialPageRoute(builder: (context) => const LoginPage());
      }

      // 3. 去个人信息页
      if (settings.name == '/profile') {
        return MaterialPageRoute(
            builder: (context) =>
                ProfilePage(userInfo: settings.arguments as Map));
      }

      // 4. 去我的房屋列表
      if (settings.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }

      // 5. 去添加房屋页面
      if (settings.name == '/location_list') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }

      // 6. 去单元页面
      if (settings.name == '/building_list') {
        Map params = settings.arguments as Map;
        return MaterialPageRoute(
          builder: (context) => BuildingList(
            point: params['point'], // 传递小区信息
          ),
        );
      }

      if (settings.name == '/room_list') {
        Map params = settings.arguments as Map;
        return MaterialPageRoute(
          builder: (context) => RoomList(
            point: params['point'], // 传递小区信息
            building: params['building'], // 传递楼栋信息
          ),
        );
      }

      if (settings.name == '/house_form') {
        return MaterialPageRoute(
            builder: (context) => HouseForm(
                  params: settings.arguments as Map,
                ));
      }

      if (settings.name == '/house_detail') {
        return MaterialPageRoute(
            builder: (context) =>
                HouseDetail(houseInfo: settings.arguments as Map));
      }

      return MaterialPageRoute(builder: (context) => const NotFound());
    },
    initialRoute: '/',
  ));
}
